//
//  SGTabBarController.m
//  MomHelp
//
//  Created by xuguoyong on 16/8/11.
//  Copyright © 2016年 hudongkeji. All rights reserved.
//

#import "SGTabBarController.h"
#import "SGTabBar.h"
#import "SGInformationViewController.h"
#import "SGHealthViewController.h"
#import "SGMeViewController.h"
#import "SGNavigationController.h"
#import "SGMessageViewController.h"



@interface SGTabBarController ()

@end

@implementation SGTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    //改变tabbar 线条颜色
    [self setTBbarSeperateColor];
    // 添加所有的子控制器
    [self addAllChildVcs];
    
    // 创建自定义tabbar
    [self addCustomTabBar];
}

/**
 *  设置tabbar线条的颜色
 */
- (void)setTBbarSeperateColor
{
   
    [self.tabBar setShadowImage:[UIImage imageNamed:@"tabbar_separte"]];
    
}

/**
 *  创建自定义tabbar
 */
- (void)addCustomTabBar
{
    // 创建自定义tabbar
   // SGTabBar *customTabBar = [[SGTabBar alloc] init];
//    customTabBar.tabBarDelegate = self;
    // 更换系统自带的tabbar
   // [self setValue:customTabBar forKeyPath:@"tabBar"];
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_background_image"];
}

/**
 *  添加所有的子控制器
 */
- (void)addAllChildVcs
{
    
   
    SGInformationViewController *information = [[SGInformationViewController alloc] init];
  
    [self addOneChlildVc:information title:@"资讯" imageName:@"tab_infomation_normal" selectedImageName:@"tab_infomation_select"];
    

    SGHealthViewController *health = [[SGHealthViewController alloc] init];
    [self addOneChlildVc:health title:@"健康" imageName:@"tab_health_normal" selectedImageName:@"tab_health_select"];
    
    SGMessageViewController *message = [[SGMessageViewController alloc] init];
    [self addOneChlildVc:message title:@"消息" imageName:@"tab_message_normal" selectedImageName:@"tab_message_select"];
   
    SGMeViewController *me = [[SGMeViewController alloc] init];
    [self addOneChlildVc:me title:@"我" imageName:@"tab_my_normal" selectedImageName:@"tab_my_select"];

}

/**
 *  添加一个子控制器
 *
 *  @param childVc           子控制器对象
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)addOneChlildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 设置标题
    childVc.title = title;
    
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 设置tabBarItem的普通文字颜色
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = UIColorFromRGB(0x999999);
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    // 设置tabBarItem的选中文字颜色
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    selectedTextAttrs[NSForegroundColorAttributeName] = UIColorFromRGB(0xFE4647);
    [childVc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
    
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    // 声明这张图片用原图(别渲染)
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = selectedImage;
    
    // 添加为tabbar控制器的子控制器
     SGNavigationController*nav = [[SGNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
}



@end
